//-----------------------说明--------------------//
//1.合并动态数组
//2.判断是否和动态数组重合
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TetrisGame
{
    public class TetrisStaticArray : MonoBehaviour
    {
        int wide;
        int high;
        int[,] tetrisArray;
        public int[,] TetrisArray { get => tetrisArray; set => tetrisArray = value; }

        public TetrisStaticArray(int wide, int high)
        {
            this.wide = wide;
            this.high = high;
            tetrisArray = new int[wide, high];
            Initialized();
        }
        public void AddArray(int[,] array, VectorToInt vector2Int)
        {
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    if (j + vector2Int.y> high)
                    {
                        break;
                    }
                    if (array[i , j ] == 1)
                    {
                        TetrisArray[i + vector2Int.x, j + vector2Int.y] = 1;
                    }

                }
            }
        }
        public bool DwonOverlap(int[,] array, VectorToInt vector2Int)
        {
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    int x = i + vector2Int.x;
                    int y = j + vector2Int.y;
                    //防止数组越界
                    if (y< high-1)
                    {
                        if (array[i, j] == 1 && TetrisArray[x, y-1] == 1)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        public bool HorizontalOverlap(int[,] array, VectorToInt vector2Int)
        {
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    int x = i + vector2Int.x;
                    int y = j + vector2Int.y;
                    Debug.Log(x);
                  
                    if (x<=0||x>= wide-1)
                    {
                        return true;
                    }
                    //Debug.Log(array[i, j]);
                    //Debug.Log("x + 1=" + (x + 1) + "/" + "y=" + y);
                    //Debug.Log(TetrisArray[x + 1, y]);
                    //防止数组越界
                    if (y < high - 1)
                    {
                        if (array[i, j] == 1 && TetrisArray[x + 1, y] == 1)
                        {
                            return true;
                        }
                        if (array[i, j] == 1 && TetrisArray[x - 1, y] == 1)
                        {
                            return true;
                        }
                    }

                }
            }
            return false;
        }
        /// <summary>
        /// 重置数组
        /// </summary>
        void Initialized()
        {
            for (int i = 0; i < wide; i++)
            {
                for (int j = 0; j < high; j++)
                {
                    TetrisArray[i, j] = 0;
                }
            }
        }
    }
}
